package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void save(AddressBook addressBook) {
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> getByCondition(AddressBook addressBook) {
        return addressBookMapper.getByCondition(addressBook);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public void setDefault(AddressBook defaultAddressBook) {
        //将原本默认地址设置为非默认状态
        AddressBook address = new AddressBook();
        address.setIsDefault(0);
        address.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(address);

        //将defaultAddressBook设置为默认地址
        defaultAddressBook.setIsDefault(1);
        addressBookMapper.updateById(defaultAddressBook);

    }
}
